import axios, { AxiosError, type Method } from 'axios'

const service = axios.create({
  // 基地址
  baseURL: 'https://api-hmzs.itheima.net/tj',
  // 超时时间
  timeout: 5000,
})
// 请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  },
)
// 响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  },
)
// . 请求工具函数
type Data<T> = {
  code: number
  message: string
  data: T
}
const request = (url: string, method: Method = 'GET', submitData?: object) => {
  return instance.request({
    url,
    method,
    [method.toUpperCase() === 'GET' ? 'params' : 'data']: submitData
  })
}



export default service
